package domain.entities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 11/09/12
 * Time: 12:50
 * To change this template use File | Settings | File Templates.
 */
public class ProtocolLevelDocumentationCategoryDAO extends HibernateDaoSupport {

    private static final Logger log = LoggerFactory
            .getLogger(ProtocolLevelDocumentationCategoryDAO.class);

    protected void initDao() {
        // do nothing
    }

    public List findAll() {
        log.debug("finding all ProtocolLevelDocumentationCategory instances");
        try {
            String queryString = "from ProtocolLevelDocumentationCategory order by descripcion asc";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public ProtocolLevelDocumentationCategory findById(Integer id) {
        log.debug("getting ProtocolLevelDocumentationCategory instance with id: " + id);
        try {
            ProtocolLevelDocumentationCategory instance = (ProtocolLevelDocumentationCategory) getHibernateTemplate()
                    .get("domain.entities.ProtocolLevelDocumentationCategory", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public void attachDirty(ProtocolLevelDocumentationCategory instance) {
        log.debug("attaching dirty ProtocolLevelDocumentationCategory instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public boolean existByDescripcion(String descripcion) {
        try {
            String queryString = "from ProtocolLevelDocumentationCategory where descripcion like '" + descripcion.trim() + "'" ;
            return (getHibernateTemplate().find(queryString).size() > 0);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public ProtocolLevelDocumentationCategory findByDescripcion(String descripcion) {
        try {
            String queryString = "from ProtocolLevelDocumentationCategory where descripcion like '" + descripcion.trim() + "'" ;

            return (ProtocolLevelDocumentationCategory)getHibernateTemplate().find(queryString).get(0);
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }


    public static ProtocolLevelDocumentationCategoryDAO getFromApplicationContext(
            ApplicationContext ctx) {
        return (ProtocolLevelDocumentationCategoryDAO) ctx
                .getBean("ProtocolInformationCategoryDAO");
    }

}
